<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    #app {
      width: 600px;
      height: 400px;
      contain: strict;
      border: 1px solid red;
      contain: strict;
      overflow-anchor: none;
      overflow-y: auto;
      content-visibility: auto;
      contain-intrinsic-size: 300px;
      scroll-behavior: smooth;


    }
  </style>
</head>

<body>
  <div id="app"></div>
  <br />
  <button>插入</button>


  <script type="module">

    const app = document.getElementById('app')
    const btn = document.querySelector('button')
    btn.addEventListener('click', () => {
      // 开始渲染
      requestAnimationFrame(render);
    })

    // 假设我们有一个需要渲染的大量数据
    const data = new Array(100_000).fill(null).map((_, index) => `Item ${index + 1}`);
    // 每次渲染的项数
    const itemsPerFrame = 100;
    // 当前渲染的起始索引
    let currentIndex = 0;
    // 渲染函数
    function render() {
      const fragment = document.createDocumentFragment();
      // 获取当前时间
      const startTime = performance.now();
      // 在每一帧中渲染尽可能多的项，直到达到 itemsPerFrame 或渲染完所有数据
      while (currentIndex < data.length && performance.now() - startTime < 16) {
        const item = data[currentIndex];
        // 这里可以是将项添加到 DOM 或其他渲染操作
        const div = document.createElement('div')
        div.textContent = currentIndex + 1
        fragment.appendChild(div)
        currentIndex++;
      }
      app.appendChild(fragment)
      // 如果还有未渲染的项，继续请求下一帧
      if (currentIndex < data.length) {
        requestAnimationFrame(render);
      } else {
        currentIndex = 0
        console.log('渲染完成');
      }
    }


  </script>
</body>

</html>